package model;

import java.util.LinkedList;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.HtmlNode;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.TagNodeVisitor;


public class GlobalDataMess {

	private LinkedList<Message> messages = new LinkedList<Message>();

	public GlobalDataMess() {
		
	}

	public void parseData(String data) {
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(data);
		
		// traverse whole DOM and update images to absolute URLs
		node.traverse(new TagNodeVisitor() {
			public boolean visit(TagNode arg0, HtmlNode htmlNode) {
		        if (htmlNode instanceof TagNode) {
		        	TagNode tag = (TagNode) htmlNode;
		            if ("tr".equals(tag.getName())) {
		            	//if(tag.getAttributeByName("class").equals("ligneMevo"))
		            	TagNode[] liste = tag.getElementsByName("td", true);
	            		String id = "";
	            		String status = "";
	            		String provenance = "";
	            		String date = "";
	            		String duration = "";
		            	for(int i=0; i<liste.length;i++) {
		            		
		            		if(liste[i].getText().toString()!="") {
			            		if(liste[i].getAttributeByName("msg_id")!=null) {
			            			id = liste[i].getAttributeByName("msg_id");
			            			messages.add(new Message(id, status, provenance, date, duration));
			            			status = "";
			            			provenance = "";
			            			date = "";
			            			duration = "";
			            		}
			            		else {
			            			if(i % 5 == 0)
			            				status = liste[i].getText().toString();
			            			if(i % 5 == 1)
			            				provenance = liste[i].getText().toString();
			            			if(i % 5 == 2)
			            				date = liste[i].getText().toString();
			            			if(i % 5 == 3)
			            				duration = liste[i].getText().toString();
			            		}
		            		}
		            	}
		            		
		            }
		        }
		        return true;
			}
		});
	}

	public LinkedList<Message> getMessages() {
		return messages;
	}

	public void setMessages(LinkedList<Message> messages) {
		this.messages = messages;
	}
}
